package com.shensi.adminCollect.controller.label;

import com.shensi.common.core.controller.BaseController;
import com.shensi.common.core.domain.AjaxResult;
import com.shensi.common.core.page.TableDataInfo;
import com.shensi.adminCollect.common.utils.StringUtils;
import com.shensi.adminCollect.common.utils.poi.ExcelUtil;
import com.shensi.adminCollect.domain.label.TimingLabel;
import com.shensi.adminCollect.service.label.ITimingLabelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

import com.shensi.common.core.domain.model.LoginUser;
import com.shensi.common.core.utils.SecurityUtils;

/**
 * 定时修改标签信息，记录Controller
 * 
 * @author qujia
 * @date 2022-11-16
 */
@RestController
@RequestMapping("/customer/label")
@Api("定时修改标签信息，记录Controller")
public class TimingLabelController extends BaseController
{
    @Autowired
    private ITimingLabelService timingLabelService;

    /**
     * 查询定时修改标签信息，记录列表
     */
    @GetMapping("/list")
    @ApiOperation("删除标签规则明细")
    public TableDataInfo list(TimingLabel timingLabel)
    {
        LoginUser current= SecurityUtils.getLoginUser();
        timingLabel.setEnterpriseId(current.getUser().getEnterpriseId());
        startPage();
        List<TimingLabel> list = timingLabelService.selectTimingLabelList(timingLabel);
        return getDataTable(list);
    }

    /**
     * 导出定时修改标签信息，记录列表
     */
    @GetMapping("/export")
    @ApiOperation("删除标签规则明细")
    public AjaxResult export(TimingLabel timingLabel)
    {
        List<TimingLabel> list = timingLabelService.selectTimingLabelList(timingLabel);
        ExcelUtil<TimingLabel> util = new ExcelUtil<TimingLabel>(TimingLabel.class);
        return util.exportExcel(list, "label");
    }

    /**
     * 获取定时修改标签信息，记录详细信息
     */
    @GetMapping(value = "/{timeId}")
    @ApiOperation("删除标签规则明细")
    public AjaxResult getInfo(@PathVariable("timeId") Long timeId)
    {
        TimingLabel timingLabel=timingLabelService.selectTimingLabelById(timeId);
        if(timingLabel!=null&&timingLabel.getDays()!=null){
            timingLabel.setDaylist(timingLabel.getDays().split(","));
        }
        return AjaxResult.success(timingLabel);
    }

    /**
     * 新增定时修改标签信息，记录
     */
    @PostMapping
    @ApiOperation("删除标签规则明细")
    public AjaxResult add(@RequestBody TimingLabel timingLabel)
    {
        LoginUser loginUser=SecurityUtils.getLoginUser();
        timingLabel.setEnterpriseId(loginUser.getUser().getEnterpriseId());
        timingLabel.setCreateDate(new Date());
        if(timingLabel.getDaylist()!=null){
            timingLabel.setDays( StringUtils.join(timingLabel.getDaylist(),","));
        }
        return toAjax(timingLabelService.insertTimingLabel(timingLabel));
    }

    /**
     * 修改定时修改标签信息，记录
     */
    @PutMapping
    @ApiOperation("删除标签规则明细")
    public AjaxResult edit(@RequestBody TimingLabel timingLabel)
    {
        timingLabel.setUpdateDate(new Date());
        if(timingLabel.getDaylist()!=null){
            timingLabel.setDays( StringUtils.join(timingLabel.getDaylist(),","));
        }
        return toAjax(timingLabelService.updateTimingLabel(timingLabel));
    }

    /**
     * 删除定时修改标签信息，记录
     */
    @DeleteMapping("/{timeIds}")
    @ApiOperation("删除标签规则明细")
    public AjaxResult remove(@PathVariable Long[] timeIds)
    {
        TimingLabel timingLabel=new TimingLabel();
        timingLabel.setDelFlag("1");
        return toAjax(timingLabelService.deleteTimingLabelByIds(timeIds));
    }
}
